define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'merchant/user/index',
                    edit_url: 'merchant/user/edit',
                    multi_url: 'merchant/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            table.on('click', '.edit_pass', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/edit_pass?id=' + id, '操作', {});
            });

            table.on('click', '.info', function() {
                let id = $(this).data('id');
                Fast.api.open('merchant/user/info/ids/' + id, '操作', {});
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'photo', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'nickname', title: '昵称'},
                        {field: 'username', title: '用户名'},
                        {field: 'phone', title: '手机号'},
                        {field: 'order_num', title: '下单数'},
                        {field: 'createtime', title: '创建时间', events: Table.api.formatter.datetime,operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'updatetime', title: '更新时间', events: Table.api.formatter.updatetime,operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'password', title: '修改密码', searchable: false, formatter: function(value, row, index) {
                            if(row.username) {
                                return '<button class="btn btn-info edit_pass" data-id="' + row.id + '">修改密码</button>';
                            } else {
                                return '';
                            }
                        }},
                        {field: 'info', title: '用户详情', searchable: false, formatter: function(value, row, index) {
                            return '<button class="btn btn-info info" data-id="' + row.id + '">查看</button>';
                        }},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]   
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        edit_pass: function () {
            Controller.api.bindevent();
            $('#show').click(function() {
                $('#c-password').attr('type', 'text');
                $('#show').hide();
                $('#hide').show();
            });

            $('#hide').click(function() {
                $('#c-password').attr('type', 'password');
                $('#show').show();
                $('#hide').hide();
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});